<?php 
/**
 * 用户控制器
 * Enter description here ...
 * @author Larry
 *
 */
class ConsoleAction extends BaseAction
{
	/**
	 * 修改用户视图
	 * Enter description here ...
	 */
	public function index()
	{	
			//当前用户的id
			$id = $_SESSION['console']['user_id'];
				
			//用户model
			$typeModel = new ConsoleModel();
			
			$result = $typeModel->getRowById($id);
			
			//前端变量注入
			$this->result = $result;
			
			$this->display();
	}
		/**
		 * 异步校验名称
		 */
		public function ajaxCheckName()
		{
		
			IS_AJAX || die('非法提交');
		
			//用户model
			$typeModel = new ConsoleModel();
			
			//过滤参数
			$field = common_html_Arr($_POST['username']);
			
			//名称校验
			$result = $typeModel->exist_name($field);
			
			
			
			
			/*if ( $result == false  )
			{
		
				$response['status'] = 1;
				$response['info'] = '用户名称可以使用';
		
			}
			else
			{
					
				$response['status'] = 0;
				$response['info'] = '用户名称已存在';
		
			}*/
		
			if ( empty($_POST['username']) )
			{
		
				$response['status'] = 0;
				$response['info'] = '用户名称不能为空';
		
			}
		
			die(json_encode($response));
		}
/**
		 * 处理编辑
		 */
		public function doUpdate()
		{
	
			//过滤请求数组
			$requestArr = common_html_Arr($_POST);
			//die(P($requestArr));
				
			//校验用户名
			if ( common_is_names($requestArr['username'], 2, 12) == NULL )
			{
				//响应状态
				$response['status'] = 0;
					
				//响应信息
				$response['info'] = '用户名格式错误';
					
				die(json_encode($response));
					
			}

			if ( empty($requestArr['password']) ) unset($requestArr['password']);
			
			
				//校验密码
				if( isset($requestArr['password']) && common_is_password($requestArr['password'], 6, 12) == NULL )
				{
					//响应状态
					$response['status'] = 0;
						
					//响应信息
					$response['info'] = '密码长度错误';
						
					die(json_encode($response));
				}
				
				$pwd1 = $requestArr['pwd1'];
				
				
				//校验两次密码
				if ( isset($requestArr['password']) && $requestArr['password'] !== $pwd1 )
				{
					//响应状态
					$response['status'] = 0;
						
					//响应信息
					$response['info'] = '两次密码不一致';
						
					die(json_encode($response));
						
				}

				
			//校验用户名
			if ( $requestArr['username'] == NULL )
			{
				
				//响应状态
				$response['status'] = 0;
					
				//响应信息
				$response['info'] = '用户名不能为空';
					
				die(json_encode($response));
					
			}
			//校验用户名
			/* 
			if ( $checkName != false && $checkName != $name['username'] )
			{
			
				//响应状态
				$response['status'] = 0;
					
				//响应信息
				$response['info'] = '用户名已存在';
					
				die(json_encode($response));
					
			} */
				
			
			//校验密码
			if ( $requestArr['password'] == NULL )
			{
					
				//响应状态
				$response['status'] = 0;
					
				//响应信息
				$response['info'] = '密码不能为空';
					
				//die(json_encode($response));

				
			}
			IS_POST || die('非法提交');
				
			isset($requestArr['password']) && $requestArr['password'] = md5($requestArr['password']);
			
			//字段数组
			$field = array(
					'user_id',
					'username',
					'password',
			);
				
			//过滤非法字段
			foreach ( $requestArr as $k=>&$v )
			{
			
				if ( !in_array($k, $field) )  unset($v);
			
			}
			
				
			//用户model
			$typeModel = new ConsoleModel();
				
			//添加分类
			$results = $typeModel->update($requestArr);
			
			die(json_encode($results));
			
		}
}


?>